Compute the summation of the absolute differenceΒΆ

Compute the summation of the absolute difference of all
distinct pairs in an given array (non-decreasing order).
Sample array:
[1, 2, 3]
Then all the distinct pairs will be:
1 2
1 3
2 3
def sum_distinct_pairs(A):
    result = 0
    i = 0
    while i<len(A):
        result += i*A[i] - (len(A)-i-1)*A[i]
        i += 1
    return result

print(sum_distinct_pairs([1, 2, 3]))
print(sum_distinct_pairs([1, 4, 5]))

Output:

4
8